import { 
  mockOverviewData,
  mockUserActivityData,
  mockProjectData,
  mockDownloadTrendData,
  mockPopularPlugins,
  mockPluginCategories,
  mockDownloadStats
} from '@/mock'
import { mockRequest } from '@/mock'

/**
 * 概览数据接口
 */
export interface OverviewData {
  totalUsers: number
  totalProjects: number
  totalPlugins: number
  totalDownloads: number
  userGrowth: number
  projectGrowth: number
  pluginGrowth: number
  downloadGrowth: number
}

/**
 * 用户活跃度数据接口
 */
export interface UserActivityData {
  dailyActiveUsers: number
  weeklyActiveUsers: number
  monthlyActiveUsers: number
  dailyGrowth: number
  weeklyGrowth: number
  monthlyGrowth: number
  labels: string[]
  data: number[]
}

/**
 * 项目数据接口
 */
export interface ProjectData {
  totalProjects: number
  totalModules: number
  totalModels: number
  monthlyChange: number
  activeProjects: number
  activeModules: number
  activeModels: number
  projectGrowth: number
  moduleGrowth: number
  modelGrowth: number
}

/**
 * 下载趋势数据接口
 */
export interface DownloadTrendData {
  labels: string[]
  downloads: number[]
  growth: number
  dateRange: string
}

/**
 * 热门插件数据接口
 */
export interface PopularPlugin {
  rank: number
  name: string
  version: string
  developer: string
  downloads: number
  growth: number
  icon: string
}

/**
 * 插件分类数据接口
 */
export interface PluginCategory {
  name: string
  count: number
  percent: number
  color: string
}

/**
 * 下载统计数据接口
 */
export interface DownloadStats {
  totalDownloads: number
  monthlyDownloads: number
  dailyDownloads: number
  peakDownloads: number
  growth: number
}

/**
 * 数据统计API服务
 */
export const statisticsApi = {
  /**
   * 获取概览数据
   */
  getOverviewData: async (): Promise<OverviewData> => {
    // 转换mock数据格式以匹配接口
    const data = {
      totalUsers: parseInt(mockOverviewData.totalUsers.count.replace(/,/g, '')),
      totalProjects: parseInt(mockOverviewData.totalProjects.count.replace(/,/g, '')),
      totalPlugins: parseInt(mockOverviewData.totalPlugins.count.replace(/,/g, '')),
      totalDownloads: parseInt(mockOverviewData.totalDownloads.count.replace(/,/g, '')),
      userGrowth: parseFloat(mockOverviewData.totalUsers.growth.replace('+', '').replace('%', '')),
      projectGrowth: parseFloat(mockOverviewData.totalProjects.growth.replace('+', '').replace('%', '')),
      pluginGrowth: parseFloat(mockOverviewData.totalPlugins.growth.replace('+', '').replace('%', '')),
      downloadGrowth: parseFloat(mockOverviewData.totalDownloads.growth.replace('+', '').replace('%', ''))
    }
    return mockRequest(data)
  },

  /**
   * 获取用户活跃度数据
   */
  getUserActivityData: async (): Promise<UserActivityData> => {
    // 转换mock数据格式以匹配接口
    const data = {
      dailyActiveUsers: parseInt(mockUserActivityData.daily.count.replace(/,/g, '')),
      weeklyActiveUsers: parseInt(mockUserActivityData.weekly.count.replace(/,/g, '')),
      monthlyActiveUsers: parseInt(mockUserActivityData.monthly.count.replace(/,/g, '')),
      dailyGrowth: parseFloat(mockUserActivityData.daily.growth.replace('%', '')),
      weeklyGrowth: parseFloat(mockUserActivityData.weekly.growth.replace('%', '')),
      monthlyGrowth: parseFloat(mockUserActivityData.monthly.growth.replace('%', '')),
      labels: mockUserActivityData.daily.labels,
      data: mockUserActivityData.daily.data
    }
    return mockRequest(data)
  },

  /**
   * 获取项目数据
   */
  getProjectData: async (): Promise<ProjectData> => {
    // 转换mock数据格式以匹配接口
    const data = {
      totalProjects: parseInt(mockProjectData.totalProjects.count.replace(/,/g, '')),
      totalModules: parseInt(mockProjectData.totalModules.count.replace(/,/g, '')),
      totalModels: parseInt(mockProjectData.totalModels.count.replace(/,/g, '')),
      monthlyChange: parseInt(mockProjectData.totalProjects.monthly.replace('+', '')),
      activeProjects: parseInt(mockProjectData.totalProjects.active.replace(/,/g, '')),
      activeModules: parseInt(mockProjectData.totalModules.basic.replace(/,/g, '')),
      activeModels: parseInt(mockProjectData.totalModels.data.replace(/,/g, '')),
      projectGrowth: parseFloat(mockProjectData.totalProjects.growthRate.replace('+', '').replace('%', '')),
      moduleGrowth: parseFloat(mockProjectData.totalModules.reuseRate.replace('%', '')),
      modelGrowth: parseFloat(mockProjectData.totalModels.usageRate.replace('%', ''))
    }
    return mockRequest(data)
  },

  /**
   * 获取下载趋势数据
   */
  getDownloadTrend: async (period: 'week' | 'month' | 'all' = 'month'): Promise<DownloadTrendData> => {
    const trendData = mockDownloadTrendData[period] || mockDownloadTrendData.month
    // 转换mock数据格式以匹配接口
    const data = {
      labels: trendData.labels,
      downloads: trendData.downloads,
      growth: trendData.growth[trendData.growth.length - 1], // 使用最后一个增长率
      dateRange: trendData.dateRange
    }
    return mockRequest(data)
  },

  /**
   * 获取热门插件数据
   */
  getPopularPlugins: async (): Promise<PopularPlugin[]> => {
    // 转换mock数据格式以匹配接口
    const data = mockPopularPlugins.map(plugin => ({
      ...plugin,
      downloads: parseInt(plugin.downloads.replace(/,/g, '')),
      growth: parseFloat(plugin.growth.replace('%', ''))
    }))
    return mockRequest(data)
  },

  /**
   * 获取插件分类数据
   */
  getPluginCategories: async (): Promise<PluginCategory[]> => {
    return mockRequest(mockPluginCategories)
  },

  /**
   * 获取下载统计数据
   */
  getDownloadStats: async (): Promise<DownloadStats> => {
    // 转换mock数据格式以匹配接口
    const data = {
      totalDownloads: parseInt(mockDownloadStats.total.replace(/,/g, '')),
      monthlyDownloads: parseInt(mockDownloadStats.month.replace(/,/g, '')),
      dailyDownloads: parseInt(mockDownloadStats.daily.replace(/,/g, '')),
      peakDownloads: parseInt(mockDownloadStats.peak.replace(/,/g, '')),
      growth: parseFloat(mockDownloadStats.growth.total.replace('+', '').replace('%', ''))
    }
    return mockRequest(data)
  }
} 